//
//  ViewController.m
//  KouyuDemo
//
//  Created by Attu on 2017/8/14.
//  Copyright © 2017年 Attu. All rights reserved.
//  首页

#import "ViewController.h"
#import "KYTestViewController.h"
#import "KYTestEngine.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) KYStartEngineConfig *startEngineConfig;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [UIView new];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    //配置初始化引擎参数
    self.startEngineConfig = [[KYStartEngineConfig alloc] init];
    
    
   #pragma mark -对应key值demo中不公开，请联系对应人
    self.startEngineConfig.appKey = @"";
    self.startEngineConfig.secretKey = @"";
    //初始化引擎
    [[KYTestEngine sharedInstance] initEngine:KY_CloudEngine startEngineConfig:self.startEngineConfig finishBlock:^(BOOL isSuccess) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
    
    [self checkCameraAuthorizationGrand:^{
        
    } withNoPermission:^{
        
    }];
}

- (void)checkCameraAuthorizationGrand:(void (^)())permissionGranted withNoPermission:(void (^)())noPermission{
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    switch (authStatus) {
        case AVAuthorizationStatusNotDetermined:
        {
            //第一次提示用户授权
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
                granted ? permissionGranted() : noPermission();
            }];
            break;
        }
        case AVAuthorizationStatusAuthorized:
        {
            //通过授权
            permissionGranted();
            break;
        }
        case AVAuthorizationStatusRestricted:
            //不能授权
            NSLog(@"不能完成授权，可能开启了访问限制");
        case AVAuthorizationStatusDenied:{
            //提示跳转到设置
        }
            break;
        default:
            break;
    }
}

#pragma mark - TableView Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identify = @"cellIdentify";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identify];
    }
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"英文单词评测";
            break;
        case 1:
            cell.textLabel.text = @"英文句子评测";
            break;
        case 2:
            cell.textLabel.text = @"英文段落评测";
            break;
            
        default:
            break;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //选择评测模式
    KYTestViewController *testVC = [self.storyboard instantiateViewControllerWithIdentifier:@"TestVC"];
    switch (indexPath.row) {
        case 0:
            testVC.testType = KYTestType_Word;
            break;
        case 1:
            testVC.testType = KYTestType_Sentence;
            break;
        case 2:
            testVC.testType = KYTestType_Paragraph;
            break;
            
        default:
            break;
    }
    
    [self.navigationController pushViewController:testVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
